package com.lyb.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyb.Dto.UserDto;
import com.lyb.mapper.UserMapper;
import com.lyb.mapper.UserRoleMapper;
import com.lyb.pojo.User;
import com.lyb.pojo.UserRole;
import com.lyb.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Override
    public User getUserByName(String name) {
        return userMapper.getUserByName(name);
    }
    public Boolean register(UserDto user){

        return userMapper.register(user);
    }

    @Override
    public PageInfo<User> queryPage(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> res = userMapper.queryPage();
        PageInfo<User> pres = new PageInfo<>(res);
        return pres;
    }

    @Override
    public Boolean del(Long id) {
        return userMapper.del(id);
    }

    @Override
    public Boolean update(User user) {
        return userMapper.update(user);
    }

    @Override
    @Transactional
    public Boolean add(User user, Long roleId) {
        userMapper.add(user);
        Integer id = user.getId();
        userRoleMapper.add(id,roleId);
        return true;
    }

    @Override
    public UserRole findByUserId(Long id) {
        return userRoleMapper.findByUserId(id);
    }
}
